<template>
  <div class="app-container">
    <div class="filter-container" style="height: 60px;">
      <el-button type="primary" size="medium" @click="$router.go(-1)">返回</el-button>
      <span class="filter-container-right">
        <el-button v-permission= "{ permissionCode: 'resource:add' }" type="primary" size="medium" icon="el-icon-plus" @click="addPermission">新增</el-button>
        <el-button v-permission= "{ permissionCode: 'resource:add' }" type="danger" size="medium" icon="el-icon-close" @click="deletePermissions">删除</el-button>
      </span>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      fit
      border
      highlight-current-row
      @selection-change="handleSelectChange">
      <el-table-column type="selection" width="40"/>
      <el-table-column align="center" label="权限名称" prop="name" min-width="150"/>
      <el-table-column align="center" label="权限编码" prop="code" min-width="150"/>
      <el-table-column align="center" label="URL" prop="url" min-width="270"/>
      <el-table-column align="center" label="是否公开" prop="is_public" min-width="120">
        <template slot-scope="scope">
          <span>
            {{ scope.row.is_public === 1 ? '是' : '否' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="描述" prop="description" min-width="100"/>
      <el-table-column align="center" label="创建日期" prop="create_date" min-width="160"/>
      <el-table-column align="center" label="修改日期" prop="modify_date" min-width="160"/>
      <el-table-column align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="editPermission(scope.row)">修改</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="deletePermission(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title=" isEdit ? '编辑权限' : '新增权限'" :visible.sync="FormVisible" width="600px">
      <el-form ref="ruleForm" :model="permissionForm" :rules="rules" >
        <el-form-item :label-width="formLabelWidth" label="权限编码" prop="code">
          <el-input v-model="permissionForm.code" :disabled="isEdit" auto-complete="off"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="权限名称" prop="name">
          <el-input v-model.number="permissionForm.name" auto-complete="off"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="URL" prop="url">
          <el-input v-model="permissionForm.url" auto-complete="off"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="是否公开">
          <el-switch v-model="permissionForm.is_public" :active-value="1" :inactive-value="0" active-color="#13ce66"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="描述">
          <el-input v-model="permissionForm.description" type="textarea" auto-complete="off"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAdd()">取 消</el-button>
        <el-button type="primary" @click="isEdit ? confirmEditPermission('ruleForm') : confirmAddPermission('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import commonApi from '@/api/commonApi'

export default {
  name: 'PermissionList',
  data() {
    return {
      resource_id: null,
      FormVisible: false,
      isEdit: false,
      list: null,
      listLoading: false,
      rules: {
        code: [
          { required: true, message: '请输入权限编码', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '请输入接口url', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' }
        ]
      },
      delPermissionIds: [],
      formLabelWidth: '120px',
      permissionForm: {},
      copyPermissionForm: {} // 修改取消时使用
    }
  },
  created() {
    this.resource_id = this.$route.params.resource_id
    this.getPermissions()
  },
  methods: {
    getPermissions() {
      this.listLoading = true
      commonApi('/permission/queryByResource', 'post', { resource_id: this.resource_id }).then(response => {
        if (response.code === 1) {
          this.list = response.data
          this.listLoading = false
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleSelectChange(valArr) {
      this.delPermissionIds = []
      for (let i = 0; i < valArr.length; i++) {
        this.delPermissionIds.push(valArr[i].id)
      }
    },
    addPermission() {
      this.isEdit = false
      this.FormVisible = true
      for (const index in this.permissionForm) {
        this.permissionForm[index] = null
      }
    },
    confirmAddPermission(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.FormVisible = false
          this.listLoading = true
          this.permissionForm.resource_id = this.resource_id
          commonApi('/permission/add', 'post', this.permissionForm).then(response => {
            if (response.code === 1) {
              this.permissionForm = {}
              this.listLoading = false
              this.getPermissions()
            }
          }).catch(() => {
            this.listLoading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    cancelAdd() {
      this.FormVisible = false
      if (!this.isEdit) {
        for (const index in this.permissionForm) {
          this.permissionForm[index] = null
        }
      } else {
        this.permissionForm = this.copyPermissionForm
      }
    },
    deletePermission(row) {
      this.$confirm(`确定要删除权限：${row.name} ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delPermissionIds.push(row.id)
        this.deletePermissionsInterim()
      }).catch(() => {
      })
    },
    deletePermissions() {
      if (this.delPermissionIds.length === 0) {
        this.$message({
          message: '请选择需要删除权限 ！',
          type: 'error'
        })
        return
      }
      this.$confirm(`确定要批量删除权限 ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.deletePermissionsInterim()
      }).catch(() => {
      })
    },
    deletePermissionsInterim() {
      this.listLoading = true
      commonApi('/permission/delete', 'post', this.delPermissionIds).then(response => {
        if (response.code === 1) {
          this.$message({
            message: '权限删除完成',
            type: 'success'
          })
          this.listLoading = false
          this.delPermissionIds = []
          this.getPermissions()
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    editPermission(row = {}) {
      this.FormVisible = true
      this.isEdit = true
      this.permissionForm = row
      this.copyPermissionForm = row
    },
    confirmEditPermission(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.FormVisible = false
          this.listLoading = true
          commonApi('/permission/update', 'post', this.permissionForm).then(response => {
            if (response.code === 1) {
              this.permissionForm = {}
              this.listLoading = false
              this.getPermissions()
            }
          }).catch(() => {
            this.listLoading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss">

</style>
